//
//  HomeWebController.m
//  好奇
//
//  Created by ***鹏辉 on 16/9/23.
//  Copyright © 2016年 Zuopenghui. All rights reserved.
//

#import "HomeWebController.h"
#import "Masonry.h"
#import "ToolBarVIew.h"
@interface HomeWebController ()<UIWebViewDelegate,UIGestureRecognizerDelegate>
@property(nonatomic,strong)UIImageView *animationView;
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)ToolBarVIew *ToolBarView;
@end

@implementation HomeWebController
-(void)viewWillAppear:(BOOL)animated
{
    [self setUpUi];
    //添加动画视图
    [self.view addSubview:self.animationView];
    [self.animationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.height.offset(100);
    }];
    //执行动画
    self.animationView.image = [UIImage animatedImageWithImages:[self loadImages] duration:3.0];
    
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];
  }
-(void)loadView{
    [super loadView];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    //添加手势
    [self setGestureRecognizer];
    //将nav改为透明
    
    UIButton *btn = [[UIButton alloc]init];
    UIBarButtonItem *btnitem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = btnitem;
}
//view加载完成后动画视图移除
-(void)viewDidAppear:(BOOL)animated
{
    [self.animationView removeFromSuperview];
}
-(void)setUpUi{
    //添加webview
    self.webView.delegate = self;
    NSURL *url1 = [NSURL URLWithString:self.url];
    NSURLRequest *request = [NSURLRequest requestWithURL:url1];
    [self.webView loadRequest:request];
    /**
     *  底部视图
     */
    UINib *nib = [UINib nibWithNibName:@"ToolBar" bundle:nil];
    self.ToolBarView = [nib instantiateWithOwner:nil options:nil].firstObject;
    __weak typeof(self) weakSelf = self;
    self.ToolBarView.BackClickBtn = ^(){
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view);
    }];

    [self.view addSubview:self.ToolBarView];
    [self.ToolBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.offset(50);
    }];
    
    

    
}
//添加上下滑动手势
-(void)setGestureRecognizer{
    UISwipeGestureRecognizer *topSwipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(TopSwipe)];
    //设置滑动的方向 （上）
    topSwipeGesture.direction=  UISwipeGestureRecognizerDirectionUp;
    topSwipeGesture.delegate = self;
    [self.view addGestureRecognizer:topSwipeGesture];
    UISwipeGestureRecognizer *DownSwipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(downSwipeGesture)];
    //设置滑动的方向 （下）
    DownSwipeGesture.direction=  UISwipeGestureRecognizerDirectionDown;
    DownSwipeGesture.delegate = self;
    [self.view addGestureRecognizer:DownSwipeGesture];
}
//允许手势共存
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}
/**
 *  向上滑动时的动画
 */
-(void)TopSwipe{
 
    NSLog(@"向上滑动");
    [self.ToolBarView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(50);
        make.height.offset(50);
    }];
    [UIView animateWithDuration:.25 animations:^{
        [self.ToolBarView layoutIfNeeded];
    }];
}
//向下滑动时的动画
-(void)downSwipeGesture{
    NSLog(@"向下滑动");
    [self.ToolBarView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.offset(50);
    }];
    [UIView animateWithDuration:.25 animations:^{
        [self.ToolBarView layoutIfNeeded];
    }];

}
//对webview数据的处理
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    NSMutableString *js = [NSMutableString string];
//    //1. 查找标签
//    [js appendString:@"var box = document.getElementsByClassName('toapp-btn')[0];"];
//    [js appendString:@"box.parentNode.removeChild(box);"];
//    
//    [js appendString:@"var load = document.getElementsByClassName('loading')[0];"];
//    [js appendString:@"load.parentNode.removeChild(load);"];
    
    [js appendString:@"var collapse = document.getElementsByClassName('com-header clearfix')[0];"];
    [js appendString:@"collapse.parentNode.removeChild(collapse);"];

    [self.webView stringByEvaluatingJavaScriptFromString:js];
}
//懒加载
-(UIWebView *)webView
{
    if (!_webView)
    {
        _webView = [[UIWebView alloc]init];
    }
    return  _webView;
}
//webview动画
-(UIImageView *)animationView{
 
    if (!_animationView)
    {
        _animationView = [UIImageView new];
    }
    return _animationView;
}
//包装动画图片
-(NSArray *)loadImages{
    NSMutableArray *Marray = [NSMutableArray array];
    for (NSInteger i = 0; i < 92; i++) {
        NSString *str = [NSString stringWithFormat:@"Loading_%05zd.png",i];
        
        UIImage *image = [UIImage imageNamed:str];
        [Marray addObject:image];
    }
    return Marray;
}
@end
